#include "stdafx.h"
#include "obstacle.h"

COREBEGIN

CMapObs::CMapObs()
{	
}

CMapObs::~CMapObs()
{

}

void	CMapObs::LoadObs(const char *ObsPath)
{
	if(!m_MapObsMatrix)
		delete m_MapObsMatrix;
	

	DWORD	dwNumber;
	bool	bValue;
	m_ObsPath = ObsPath;
	FILE *pFile;
	if (fopen_s(&pFile,ObsPath,"rb+"))
	{
		fread(&m_wdCols,sizeof(m_wdCols),1,pFile);
		fread(&m_wdRows,sizeof(m_wdRows),1,pFile);
		m_MapObsMatrix = new MOM(m_wdCols,m_wdRows);
		MOMNode Node = m_MapObsMatrix->begin();
		while(1)
		{
			if(!fread(&dwNumber,sizeof(DWORD),1,pFile))
				break;
			fread(&bValue,sizeof(bool),1,pFile);
			for (DWORD i = 0; i < dwNumber; i ++)
			{
				(*Node) = bValue;
				Node++;
			}
		}
		fclose(pFile);
	}

	
}
void	CMapObs::SaveObs(const char *ObsPath)
{
	if(ObsPath != NULL)
		m_ObsPath = ObsPath;
	bool	bValue;
	DWORD	dwNumber = 0;
	FILE *pFile;
	if (fopen_s(&pFile,m_ObsPath.c_str(),"wb+") == 0)
	{
		fwrite(&m_wdCols,sizeof(WORD),1,pFile);
		fwrite(&m_wdRows,sizeof(WORD),1,pFile);
		MOMNode Node = m_MapObsMatrix->begin();
		bValue = (*Node);
		while(Node != m_MapObsMatrix->end())
		{
			if (bValue == (*Node))
				dwNumber++;
			else
			{
				fwrite(&dwNumber,sizeof(DWORD),1,pFile);
				fwrite(&bValue,sizeof(bool),1,pFile);
				bValue = (*Node);
				dwNumber = 1;
			}
			Node++;
		}
		fclose(pFile);
	}

}
bool	CMapObs::IsObs(_Vector3D	v)
{
	return true;
}
bool	CMapObs::IsObs(float x,float z)
{
	return true;
}
WORD	CMapObs::getRows()
{
	return m_wdRows;
}
WORD	CMapObs::getCols()
{
	return m_wdCols;
}
void	CMapObs::setValue(float x,float z,bool bValue)
{
	MOMNode Node = m_MapObsMatrix->find(x,z);
	if(Node != m_MapObsMatrix->end())
		(*Node) = bValue;
}
COREEND

